package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.service;

import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdeks_gettoken;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_screen_VideoSteam;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class Frontdesk_screen_VideoStreamService {



public String gettoken(String pid,Long currentTimeSeconds){
    Frontdeks_gettoken frontdeksGettoken = new Frontdeks_gettoken();
    frontdeksGettoken.setChannel("1551505304@qq.com");
    frontdeksGettoken.setPId(pid);
    frontdeksGettoken.setCId(1);
    frontdeksGettoken.setGenType("url");
    frontdeksGettoken.setDes("刘奥旸测试");
    frontdeksGettoken.setExpiredSec(0L);
    frontdeksGettoken.setSecret("89e094fe21278503d8d0010af9545231");
//    frontdeksGettoken.setT(1732587195L);
    frontdeksGettoken.setT(currentTimeSeconds);

    String strToHash = "cId=" + frontdeksGettoken.getCId()
            + "&channel=" + frontdeksGettoken.getChannel()
            + "&des=" + frontdeksGettoken.getDes()
            + "&expireldSec=" + frontdeksGettoken.getExpiredSec()
            + "&genType=" + frontdeksGettoken.getGenType()
            + "&pId=" + frontdeksGettoken.getPId()
            + "&secret=" + frontdeksGettoken.getSecret()
            + "&t=" + frontdeksGettoken.getT();

    // 返回加密后的token
    return md5Hex(strToHash);
}



//得到全部的
    public Frontdesk_screen_VideoSteam getVideoSteam(String pid){
        long currentTimeMillis = System.currentTimeMillis();
        // 将毫秒级时间戳转换为秒级时间戳
        long currentTimeSeconds = currentTimeMillis / 1000;

        Frontdesk_screen_VideoSteam frontdeskScreenVideoSteam = new Frontdesk_screen_VideoSteam();
        frontdeskScreenVideoSteam.setChannel("1551505304@qq.com");
        frontdeskScreenVideoSteam.setPId(pid);
        frontdeskScreenVideoSteam.setCId(1);
        frontdeskScreenVideoSteam.setGenType("url");
        frontdeskScreenVideoSteam.setDes("刘奥旸测试");
        frontdeskScreenVideoSteam.setExpiredSec(0L);
        frontdeskScreenVideoSteam.setToken(this.gettoken(pid,currentTimeSeconds));
        frontdeskScreenVideoSteam.setT(currentTimeSeconds);

        return frontdeskScreenVideoSteam;
    }









//md5方法：
    private String md5Hex(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(input.getBytes());
            return byteArrayToHex(hashBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String byteArrayToHex(byte[] byteArray) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : byteArray) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}
